#include<iostream>
using namespace std;
const int N = 5e4 + 10;
int d[N];
int p[N];
int n, k;
int mod(int a, int b)
{
	return (a % b + b) % b;
}
int find(int x)
{
	if (x != p[x]) {
		int root = find(p[x]);
		d[x] = mod(d[x] + d[p[x]], 3);
		p[x] = root;
	}
	return p[x];
}
void merge(int x, int y, int t)
{
	int px = find(x);
	int py = find(y);
	d[px] = mod((t - d[x] + d[y]), 3);
	p[px] = py;
}
int main()
{
	cin >> n >> k;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		p[i] = i;
	}
	for (int i = 1; i <= k; i++) {
		{
			int D, x, y;
			cin >> D >> x >> y;
			if (x > n || y > n) {
				ans++;
				continue;
			}
			if (D == 1) {
				int px = find(x);
				int py = find(y);
				if (px == py) {
					if (mod(d[x],3) != (mod(d[y],3))) {
						ans++;
					}
				}
				else {
					merge(x, y, 0);
				}
			}
			else {
				if (x == y) {
					ans++;
					continue;
				}
				int px = find(x);
				int py = find(y);
				if (px == py) {
					int t = mod(d[x] - d[y], 3);
					if (t != 1) {
						ans++;
					}
				}
				else {
					merge(x, y, 1);
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}